package com.aidong.carnival.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aidong.activity.dao.MstCampaignDao;
import com.aidong.activity.entity.MstCampaign;
import com.aidong.carnival.controllers.concerns.Failure;
import com.aidong.carnival.dao.ActivityDao;
import com.aidong.carnival.dao.InvitationDao;
import com.aidong.carnival.entity.Activity;
import com.aidong.carnival.entity.Message;
import com.aidong.carnival.service.ActivityService;
import com.aidong.carnival.service.AllCampaignService;
import com.aidong.food.dao.MstFoodDao;
import com.aidong.food.dao.TrnFoodBuyDao;
import com.aidong.jobs.constants.JobConst;
import com.aidong.myprofile.dao.MyAddressDao;
import com.aidong.pay.config.util.ClenderUtil;
import com.aidong.pay.dao.OrdergeneratedDao;
import com.aidong.stadium.dao.MstScheduleDao;
import com.aidong.stadium.entity.MstConclusionabout;
import com.aidong.stadium.entity.MstSchedule;
import com.aidong.util.CalendarUtil;
import com.aidong.util.bus.CodeConsts;
import com.aidong.util.bus.SecurityUtils;

@Service
public class AllCampaignServiceImpl implements AllCampaignService {

	@Autowired
	ActivityDao activityDao;

	@Autowired
	InvitationDao invitationDao;

	@Autowired
	MstCampaignDao mstCampaignDao;
	@Autowired
	private OrdergeneratedDao ordergeneratedDao;

	@Autowired
	private TrnFoodBuyDao trnFoodBuyDao;

	@Autowired
	private MyAddressDao myAddressDao;
	@Autowired
	private MstFoodDao mstFoodDao;

	@Autowired
	private MstScheduleDao aboutScheduleDao;

	/*
	 * 商品购买 (non-Javadoc)
	 * 
	 * @see com.aidong.carnival.service.AllCampaignService#addFoodBuy(int[],
	 * int, int, int, java.lang.String, int, java.lang.String, java.lang.String)
	 */
	@Transactional
	@Override
	public Message addFoodBuy(int ruleId, int goodsType, int quantity, int addressId, String buyDate, int IdongId,
			String contactName, String contactPhone, int pickUpAway) throws Exception {
		Message ms = new Message();
		Map<String, Object> map = new HashMap<String, Object>();
		String buyYearDate = new SimpleDateFormat("yyyy").format(new Date()) + "-" + buyDate;
		int selectStoreId = 0;
		// 订单规则信息
		Map<String, Object> paraMap = trnFoodBuyDao.getOrderRuleInfo(ruleId);// 获取供应规则信息
		paraMap.put("idongId", IdongId);
		paraMap.put("contactName", contactName);
		paraMap.put("contactPhone", contactPhone);
		paraMap.put("quantity", quantity);
		paraMap.put("buyDate", buyYearDate);
		paraMap.put("goodsType", goodsType);

		double unitPrice = Double.parseDouble(paraMap.get("price").toString());
		selectStoreId = Integer.parseInt(paraMap.get("storeId").toString());
		double amount = unitPrice * quantity;
		String province, city, country, detailAddress, address = "", receiverName = "", mobilePhone = "";
		if (addressId > 0) {
			// storeId
			paraMap.put("storeId", 1853);// m上1853是爱动健身
			List<Map<String, Object>> addressInfo = new ArrayList<Map<String, Object>>();
			paraMap.put("autoId", addressId);
			addressInfo = myAddressDao.getReceiverAddress(paraMap);
			province = addressInfo.get(0).get("province").toString();
			city = addressInfo.get(0).get("city").toString();
			country = addressInfo.get(0).get("country").toString();
			detailAddress = addressInfo.get(0).get("detailAddress").toString();
			address = province + city + country + detailAddress;
			receiverName = addressInfo.get(0).get("receiverName").toString();
			mobilePhone = addressInfo.get(0).get("mobilePhone").toString();
			pickUpAway=2;
		}
		paraMap.put("amount", amount);
		paraMap.put("pickUpStoreId", selectStoreId);
		paraMap.put("addressId", addressId);
		paraMap.put("pickUpWay", pickUpAway);
		paraMap.put("recommendCoach", "");
		paraMap.put("address", address);
		paraMap.put("nameAddress", receiverName);
		paraMap.put("mobileAddress", mobilePhone);
		if (goodsType == 3) {
			paraMap.put("orderType", 3);
		} else if (goodsType == 4) {
			paraMap.put("orderType", 4);
		} else if (goodsType == 5) {
			paraMap.put("orderType", 5);
		}

		Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("status", 0);
		statusMap.put("idongId", paraMap.get("idongId").toString());
		trnFoodBuyDao.editBuyFoodFlg(statusMap);

		trnFoodBuyDao.addFoodBuy(paraMap);
		trnFoodBuyDao.addFoodBuyOrder(paraMap);

		// trnFoodBuyBiz.addFoodBuy(paraMap);//餐饮购买表插入数据-选中的送货地址ID、选中的取货方式、门店ID

		//Map<String, Object> resultMap = new HashMap<String, Object>();
		map.put("subscribeId", paraMap.get("subscribeId"));
		map.put("orderId", paraMap.get("orderId"));
		map.put("quantity", quantity);
		map.put("goodsType", goodsType);
		map.put("amount", amount);
		map.put("selectPickUpWay", pickUpAway);
		map.put("kuaidiPrice", 0);
		map.put("foodName", paraMap.get("tradeName"));
		map.put("brandId", paraMap.get("brandId"));
		map.put("foodId", paraMap.get("foodId"));

		map.put("code", 1);
		List<Map<String, Object>> payInfo = getPayInfo(mstFoodDao.getPayInfo(ruleId));
		map.put("zhifubao", payInfo.get(0).get("zhifubao"));
		map.put("weixin", payInfo.get(1).get("weixin"));
		map.put("voucher", payInfo.get(2).get("voucher"));
		map.put("exchange", payInfo.get(3).get("exchange"));
		map.put("membership", payInfo.get(4).get("membership"));
		map.put("discount", payInfo.get(5).get("discount"));
		//map.put("result", resultMap);
		ms.setCode(1);
		ms.setMap(map);
		return ms;
	}

	/*
	 * 活动购买 (non-Javadoc)
	 * 
	 * @see com.aidong.carnival.service.AllCampaignService#addCampaignBuy(int,
	 * int, int, java.lang.String, java.lang.String)
	 */
	@Transactional
	@Override
	public Message addCampaignBuy(int ruleId, int addressId, int idongId, String contactName, String contactPhone)
			throws Exception {
		MstCampaign campaign = new MstCampaign();
		Message ms = new Message();
		Map<String, Object> map = new HashMap<String, Object>();
		campaign.setIdongId(idongId + ""); // 爱动号
		campaign.setContactName(contactName);
		campaign.setContactPhone(contactPhone);
		campaign.setCampaignId(ruleId);
		int campaignId = ruleId;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String sysDate = sdf.format(new Date());
		Date formatDate = sdf.parse(sysDate); // 系统当前年月日

		// 获得活动报名结束时间、报名人数、可参加人数
		Map<String, Object> msgMap = mstCampaignDao.getCampaignSignMsg(campaignId);
		campaign.setCampaignTitle(msgMap.get("title").toString());
		campaign.setCampaignTime(msgMap.get("campaignTime").toString());
		
		int version = Integer.parseInt(msgMap.get("version").toString());// version
																			// 字段
		String signStratTime = msgMap.get("stratSignTime").toString(); // 活动报名开始时间
		Date stratDateTime = sdf.parse(signStratTime);
		String signEndTime = msgMap.get("endSignTime").toString(); // 活动报名截止时间
		Date endDateTime = sdf.parse(signEndTime);
		int allowPerson = Integer.parseInt(msgMap.get("allowPerson").toString()); // 活动可报名人数
		int signPerson = mstCampaignDao.getCampaignValidCount(campaignId); // 活动报名有效人数（已报名人数）

		// add 2016-02-18 调用存储过程判断是否可以报名需要的参数
		Map<String, Object> paraMap1 = new HashMap<String, Object>();
		paraMap1.put("campaignId", campaignId);
		paraMap1.put("allowPersonLocal", allowPerson);
		paraMap1.put("version", version);
		if (allowPerson > signPerson) {
			if (formatDate.compareTo(endDateTime) == 1 || formatDate.compareTo(stratDateTime) == -1) {
				ms.setCode(0);
				ms.setMessage("当前时间不在报名时间范围之内");
				return ms;
			} else {

				// 判断该手机是否已经报名
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("campaignId", campaignId);
				paraMap.put("idongId", idongId);
				paraMap.put("version", version);
				List<Map<String, Object>> alreayMap = mstCampaignDao.getIfAlreadyReg(paraMap);
				if (alreayMap.size() > 0) { // 是否已有报名记录
					for (Map<String, Object> alreay : alreayMap) {
						int alreayStatus = Integer.parseInt(alreay.get("status").toString());
						if (alreayStatus == CodeConsts.CODE_STATIC_BOOKING_BOOKING) { // 已预约状态
							ms.setCode(0);
							ms.setMessage("您已报名该活动，请到订单列表查看");
							return ms;
						}
					}
					Map<String, Object> para = new HashMap<String, Object>();
					para.put("campaignId", campaignId);
					MstCampaign campaignDetail = mstCampaignDao.getFullByCampaignId(para);
					if (campaignDetail == null) {
						throw new Exception();
					} else if (Integer.parseInt(campaignDetail.getIsFull()) == 1) {
						ms.setCode(0);
						ms.setMessage("预约人数已满");
						return ms;
					}

					paraMap.put("signPerson", signPerson);
					paraMap.put("allowPerson", allowPerson);

					int result = mstCampaignDao.updateFullByCampaignId(paraMap);
					if (result == 0) {
						ms.setCode(0);
						ms.setMessage("当前预约人数较多，请稍后重试!");
						return ms;
					}
					if(addressId>0){
						//根据地址ID获取地址信息
						Map<String,Object> addMap=mstCampaignDao.getAddressDetailById(addressId);
						if(addMap!=null){
							campaign.setAddress(addMap.get("address").toString());
							campaign.setNameAddress(addMap.get("name").toString());
							campaign.setMobileAddress(addMap.get("phone").toString());
							campaign.setSelectAddressId(addressId);
						}
					}
					// 添加预约信息
					mstCampaignDao.addActivityReg(campaign);
					int subscribeId = campaign.getSubscribeId(); // 预约ID

					// 添加付款信息
					Map<String, Object> payMap = new HashMap<String, Object>();
					payMap.put("subscribeId", subscribeId);
					payMap.put("orderType", 1);
					payMap.put("title",campaign.getCampaignTitle());
					payMap.put("idongId", campaign.getIdongId());
					payMap.put("nickName", campaign.getContactName());
					payMap.put("phone", campaign.getContactPhone());
					//Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
					payMap.put("amount", msgMap.get("price"));

					if (Double.parseDouble(msgMap.get("price").toString()) > 0) {
						payMap.put("status", 0);
						payMap.put("processId", 1);
					} else {
						payMap.put("status", 1);
						payMap.put("processId", 2);
						payMap.put("tickNo", ClenderUtil.electronicTicketNo(campaign.getContactPhone())); // 电子券号
					}
					ordergeneratedDao.addCarnivalCampaginOrderPay(payMap);
					// ordergeneratedDao.addCampaginOrderPay(payMap);
					String orderId = payMap.get("orderId").toString();

					map.put("price", Double.parseDouble(msgMap.get("price").toString()));
					map.put("subscribeId", subscribeId);
					map.put("orderId", orderId);
					map.put("campaignId", campaign.getCampaignId());
					map.put("CampaignStartTime", campaign.getCampaignTime());
					map.put("title", campaign.getCampaignTitle());
					map.put("place", msgMap.get("place").toString());
					map.put("name", campaign.getContactName());
					map.put("phone", campaign.getContactPhone());
					map.put("goodsType",1);
					List<Map<String, Object>> payInfo = getPayInfo(mstCampaignDao.getPayInfo(campaignId));
					map.put("zhifubao", payInfo.get(0).get("zhifubao"));
					map.put("weixin", payInfo.get(1).get("weixin"));
					map.put("voucher", payInfo.get(2).get("voucher"));
					map.put("exchange", payInfo.get(3).get("exchange"));
					map.put("membership", payInfo.get(4).get("membership"));
					map.put("discount", payInfo.get(5).get("discount"));
					ms.setCode(1);
					ms.setMap(map);
					return ms;
				} else {
					Map<String, Object> para = new HashMap<String, Object>();
					para.put("campaignId", campaignId);
					MstCampaign campaignDetail = mstCampaignDao.getFullByCampaignId(para);
					if (campaignDetail == null) {
						throw new Exception();
					} else if (Integer.parseInt(campaignDetail.getIsFull()) == 1) {
						ms.setCode(0);
						ms.setMessage("预约人数已满!");
						return ms;
					}
					paraMap.put("signPerson", signPerson);
					paraMap.put("allowPerson", allowPerson);

					int result = mstCampaignDao.updateFullByCampaignId(paraMap);
					if (result == 0) {
						ms.setCode(0);
						ms.setMessage("当前预约人数较多，请稍后重试!");
						return ms;
					}
					if(addressId>0){
						//根据地址ID获取地址信息
						Map<String,Object> addMap=mstCampaignDao.getAddressDetailById(addressId);
						if(addMap!=null){
							campaign.setAddress(addMap.get("address").toString());
							campaign.setNameAddress(addMap.get("name").toString());
							campaign.setMobileAddress(addMap.get("phone").toString());
							campaign.setSelectAddressId(addressId);
						}
					}
					mstCampaignDao.addActivityReg(campaign);
					int subscribeId = campaign.getSubscribeId(); // 预约ID

					// 添加付款信息
					Map<String, Object> payMap = new HashMap<String, Object>();
					payMap.put("subscribeId", subscribeId);
					payMap.put("orderType", 1);
					payMap.put("title",campaign.getCampaignTitle());
					payMap.put("idongId", campaign.getIdongId());
					payMap.put("nickName", campaign.getContactName());
					payMap.put("phone", campaign.getContactPhone());
					//Map<String, Object> priceMap = ordergeneratedDao.getActivityPrice(subscribeId);
					payMap.put("amount", msgMap.get("price"));
					if (Double.parseDouble(msgMap.get("price").toString()) > 0) {
						payMap.put("status", 0);
						payMap.put("processId", 1);
					} else {
						payMap.put("status", 1);
						payMap.put("processId", 2);
						payMap.put("tickNo", ClenderUtil.electronicTicketNo(campaign.getContactPhone())); // 电子券号
					}
					ordergeneratedDao.addCarnivalCampaginOrderPay(payMap);
					// ordergeneratedDao.addCampaginOrderPay(payMap);
					String orderId = payMap.get("orderId").toString();
					map.put("price", Double.parseDouble(msgMap.get("price").toString()));
					map.put("subscribeId", subscribeId);
					map.put("orderId", orderId);
					map.put("campaignId", campaign.getCampaignId());
					map.put("CampaignStartTime", campaign.getCampaignTime());
					map.put("title", campaign.getCampaignTitle());
					map.put("place", msgMap.get("place").toString());
					map.put("name", campaign.getContactName());
					map.put("phone", campaign.getContactPhone());
					map.put("goodsType",1);
					List<Map<String, Object>> payInfo = getPayInfo(mstCampaignDao.getPayInfo(campaignId));
					map.put("zhifubao", payInfo.get(0).get("zhifubao"));
					map.put("weixin", payInfo.get(1).get("weixin"));
					map.put("voucher", payInfo.get(2).get("voucher"));
					map.put("exchange", payInfo.get(3).get("exchange"));
					map.put("membership", payInfo.get(4).get("membership"));
					map.put("discount", payInfo.get(5).get("discount"));
					// }
					ms.setCode(1);
					ms.setMap(map);
					return ms;
					
				}
			}
		} else/* if (allowPerson <= signPerson) */ {
			ms.setCode(0);
			ms.setMessage("报名人数已满!");
			return ms;
		}
	}

	/*
	 * 课程购买
	 * (non-Javadoc)
	 * @see com.aidong.carnival.service.AllCampaignService#addScheduleBuy(int, int)
	 */
	@Override
	public Message addScheduleBuy(int ruleId, int idongId,String phone,String name) throws Exception {
		MstSchedule schedule = new MstSchedule();
		Message ms = new Message();
		Map<String, Object> map = new HashMap<String, Object>();
		// 解密爱动号
		schedule.setIdongId(idongId); // 爱动号
		Map<String, Object> subsc = new HashMap<String, Object>();
		subsc.put("autoId", ruleId);
		MstSchedule subscribe = aboutScheduleDao.getSubscribeDetail(subsc);

		schedule.setAutoId(subscribe.getAutoId());
		schedule.setStoreId(subscribe.getStoreId());
		schedule.setCourseId(subscribe.getCourseId());
		schedule.setSubscribeDate(subscribe.getSubscribeDate());

		int courseId = schedule.getCourseId(); // 课程ID
		int autoId = schedule.getAutoId(); // 团操时间ID
		// 解密联系人电话
		schedule.setContactPhone(phone);
		schedule.setContactName(name);
		String contactPhone = schedule.getContactPhone(); // 联系人电话
		String subscribeDate = schedule.getSubscribeDate(); // 预约日期

		subscribeDate = CalendarUtil.courseDateFomat(subscribeDate);
		schedule.setSubscribeDate(subscribeDate); // 预约日期

		// 指定可提前预约天数
		Map<String, Object> callableMap = aboutScheduleDao.getCallableBondDay(autoId);
		int duleDay = Integer.parseInt(callableMap.get("duleDay").toString());

		// 最早预约时间
		SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = dft.parse(subscribeDate);
		Calendar date = Calendar.getInstance();
		date.setTime(beginDate);
		date.set(Calendar.DATE, date.get(Calendar.DATE) - duleDay);
		String earlyDate = dft.format(date.getTime());
		String sysSubDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		if (sysSubDate.compareTo(earlyDate) < 0) {
			ms.setCode(0);
			ms.setMessage("该课程暂时不能预约!");
			return ms;
		}

		// 所有课程只能提前十五分钟预约
		String sysDate = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
		long minutes = CalendarUtil.fromDateStringToLong(sysDate, subscribeDate); // 预约时间
		boolean results=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(subscribeDate).before(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(sysDate));
		if (Math.abs(minutes) < 15 || results) {
			ms.setCode(0);
			ms.setMessage("该课程需提前15分钟预约!");
			return ms;
		}

		// 判断该手机号码是否在该时间段预约其它课程
		Map<String, Object> otherPramMap = new HashMap<String, Object>();
		otherPramMap.put("phoneNo", contactPhone);
		otherPramMap.put("autoId", autoId);
		List<Map<String, Object>> otherMap = aboutScheduleDao.getOtherAppoint(otherPramMap);

		for (Map<String, Object> otherCourse : otherMap) {
			String year = otherCourse.get("subscribeDate") + ""; // 年月日
			String startTime = otherCourse.get("startTime") + ""; // 课程开始时间
			String endTime = otherCourse.get("endTime") + ""; // 课程结束时间

			String courseStartTime = year + " " + startTime;
			String courseEndTime = year + " " + endTime;

			if (subscribeDate.compareTo(courseStartTime) > -1 && subscribeDate.compareTo(courseEndTime) < 1) {
				ms.setCode(0);
				ms.setMessage("您已在该时间段预约其它课程!");
				return ms;
			}
		}

		// 可预约人数
		Map<String, Object> allPersonMap = new HashMap<String, Object>();
		allPersonMap.put("courseId", courseId); // 团操课程ID
		allPersonMap.put("autoId", autoId); // 时间ID
		Map<String, Object> resultMap = aboutScheduleDao.getAppointIfFull(allPersonMap);
		int personCount = Integer.valueOf(resultMap.get("allowPerson").toString());
		int version = Integer.valueOf(resultMap.get("version").toString());
		// 已预约人数
		Map<String, Object> paraAppointMap = new HashMap<String, Object>();
		paraAppointMap.put("courseId", courseId); // 课程ID
		paraAppointMap.put("autoId", autoId); // 时间ID
		int alreadyPerson = aboutScheduleDao.getAppointOfPerson(paraAppointMap);

		if (personCount <= alreadyPerson) {
			ms.setCode(0);
			ms.setMessage("预约已满，您可以选择别的课程!");
			return ms;
		} else/* if (personCount > alreadyPerson) */ {
			// 判断该手机是否在当天已经预约该课程
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("autoId", autoId);
			paraMap.put("idongId", idongId);
			paraMap.put("subscribeDate", subscribeDate);
			List<Map<String, Object>> alreayMap = aboutScheduleDao.getIfAppoint(paraMap);
			if (alreayMap.size() > 0) {
				for (Map<String, Object> alreay : alreayMap) {
					int alreayStatus = Integer.parseInt(alreay.get("status").toString());
					if (alreayStatus == 0) { // 已预约
						ms.setCode(0);
						ms.setMessage("你已预约该课程，请到订单列表查看!");
						return ms;
					}
				}

				Map<String, Object> updateMap = new HashMap<String, Object>();
				updateMap.put("courseId", courseId); // 课程ID
				updateMap.put("autoId", autoId); // 时间ID
				updateMap.put("version", version); // version

				int result = aboutScheduleDao.editScheduleVersion(updateMap);
				int subscribeId = 0;// 预约ID
				String orderId = "";// 订单ID
				if (result > 0) {
					aboutScheduleDao.addScheduleDuce(schedule);
					subscribeId = schedule.getSubscribeId(); // version更新成功获取
					// 添加付款信息
					Map<String, Object> payMap = new HashMap<String, Object>();
					payMap.put("subscribeId", subscribeId);
					payMap.put("orderType", 2);
					payMap.put("storeId", schedule.getStoreId());
					Map<String,Object> price=aboutScheduleDao.getSubscribePrice(ruleId);
					if(Double.parseDouble(price.get("price").toString())>0){
						payMap.put("status", 0);
						payMap.put("processId", 1);
					}else{
						payMap.put("status", 1);
						payMap.put("processId", 2);
						payMap.put("tickNo",ClenderUtil.electronicTicketNo(phone)); // 电子券号
					}
					ordergeneratedDao.addCourseOrderPayNew(payMap);
					orderId = payMap.get("orderId").toString(); // 订单ID
				}

				if (result > 0) {
					MstConclusionabout ab=aboutScheduleDao.getScheduleDuce(ruleId);
					map.put("price",ab.getPrice());
					map.put("subscribeId", subscribeId);
					map.put("orderId", orderId);
					map.put("campaignId", ruleId);
					map.put("CampaignStartTime", ab.getClassTime()+" "+ab.getStartTime()+"-"+ab.getEndTime());
					map.put("title", ab.getChName());
					map.put("place", ab.getClassRoom());
					map.put("name", name);
					map.put("phone", phone);
					map.put("goodsType", 2);
					List<Map<String, Object>> payInfo = getPayInfo(aboutScheduleDao.getPayInfo(autoId));
					map.put("zhifubao", payInfo.get(0).get("zhifubao"));
					map.put("weixin", payInfo.get(1).get("weixin"));
					map.put("voucher", payInfo.get(2).get("voucher"));
					map.put("exchange", payInfo.get(3).get("exchange"));
					map.put("membership", payInfo.get(4).get("membership"));
					map.put("discount", payInfo.get(5).get("discount"));
					ms.setCode(1);
					ms.setMap(map);
					return ms;
				} else {
					/*ms.setCode(0);
					ms.setMessage("当前预约人数较多，请稍后重试!");
					return ms;*/
					throw new UnsupportedOperationException();
				}

			} else { // 未预约
				Map<String, Object> updateMap = new HashMap<String, Object>();
				updateMap.put("courseId", courseId); // 课程ID
				updateMap.put("autoId", autoId); // 时间ID
				updateMap.put("version", version); // version

				int result = aboutScheduleDao.editScheduleVersion(updateMap);
				int subscribeId = 0;// 预约ID
				String orderId = "";// 订单ID
				if (result > 0) {
					aboutScheduleDao.addScheduleDuce(schedule);
					subscribeId = schedule.getSubscribeId(); // version更新成功获取
					// 添加付款信息
					Map<String, Object> payMap = new HashMap<String, Object>();
					payMap.put("subscribeId", subscribeId);
					payMap.put("orderType", 2);
					payMap.put("storeId", schedule.getStoreId());
					Map<String,Object> price=aboutScheduleDao.getSubscribePrice(ruleId);
					if(Double.parseDouble(price.get("price").toString())>0){
						payMap.put("status", 0);
						payMap.put("processId", 1);
					}else{
						payMap.put("status", 1);
						payMap.put("processId", 2);
						payMap.put("tickNo",ClenderUtil.electronicTicketNo(phone)); // 电子券号
					}
					ordergeneratedDao.addCourseOrderPayNew(payMap);
					//ordergeneratedDao.addCourseOrderPay(payMap);
					orderId = payMap.get("orderId").toString(); // 订单ID
				}

				if (result > 0) {
					MstConclusionabout ab=aboutScheduleDao.getScheduleDuce(ruleId);
					map.put("price",ab.getPrice());
					map.put("subscribeId", subscribeId);
					map.put("orderId", orderId);
					map.put("campaignId", ruleId);
					map.put("CampaignStartTime", ab.getClassTime()+" "+ab.getStartTime()+"-"+ab.getEndTime());
					map.put("title", ab.getChName());
					map.put("place", ab.getClassRoom());
					map.put("name", name);
					map.put("phone", phone);
					map.put("goodsType", 2);
					List<Map<String, Object>> payInfo = getPayInfo(aboutScheduleDao.getPayInfo(autoId));
					map.put("zhifubao", payInfo.get(0).get("zhifubao"));
					map.put("weixin", payInfo.get(1).get("weixin"));
					map.put("voucher", payInfo.get(2).get("voucher"));
					map.put("exchange", payInfo.get(3).get("exchange"));
					map.put("membership", payInfo.get(4).get("membership"));
					map.put("discount", payInfo.get(5).get("discount"));
					ms.setCode(1);
					ms.setMap(map);
					return ms;
				} else {
					throw new UnsupportedOperationException();
					/*ms.setCode(0);
					ms.setMessage("当前预约人数较多，请稍后重试!");
					return ms;*/
				}

			}

		}
	}

	/**
	 * 得到支付信息
	 * 
	 * @param campaignId
	 * @return
	 */
	public List<Map<String, Object>> getPayInfo(String payInfo) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Object> zhifubao = new HashMap<String, Object>();
		Map<String, Object> weixin = new HashMap<String, Object>();
		Map<String, Object> voucher = new HashMap<String, Object>();
		Map<String, Object> exchange = new HashMap<String, Object>();
		Map<String, Object> membership = new HashMap<String, Object>();
		Map<String, Object> discount = new HashMap<String, Object>();
		if (payInfo.contains("1")) {
			zhifubao.put("zhifubao", true);
			result.add(zhifubao);
		} else {
			zhifubao.put("zhifubao", false);
			result.add(zhifubao);
		}
		if (payInfo.contains("2")) {
			weixin.put("weixin", true);
			result.add(weixin);
		} else {
			weixin.put("weixin", false);
			result.add(weixin);
		}
		if (payInfo.contains("3")) {
			voucher.put("voucher", true);
			result.add(voucher);
		} else {
			voucher.put("voucher", false);
			result.add(voucher);
		}
		if (payInfo.contains("4")) {
			exchange.put("exchange", true);
			result.add(exchange);
		} else {
			exchange.put("exchange", false);
			result.add(exchange);
		}
		if (payInfo.contains("5")) {
			membership.put("membership", true);
			result.add(membership);
		} else {
			membership.put("membership", false);
			result.add(membership);
		}
		if (payInfo.contains("6")) {
			discount.put("discount", true);
			result.add(discount);
		} else {
			discount.put("discount", false);
			result.add(discount);
		}
		return result;
	}
}
